gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\linear\fisher\fishdemo.m
function []=fishdemo(action,hfigure,varargin) % FISHDEMO demo on algorithms which learn Fisher's classifer. % % FISHDEMO demonstrates use of algorithms finding the Fisher's % classifier. The task is to split the feature space to % convex cones, each cone corresponds to one class. % Formaly, the task is to find such vectors alphas(:,i), % i=1,2,...K that inequalities % % alphas(:,i)'*X(:,k) > alphas(:,j)'*X(:,k) for any J(k)=i,i~=j. % % hold. Underlying idea is to transform k-class problem to % 2-class (or binary) classification problem. % % The algorithms are demonstrated in 2D space. The found linear % decision boundaries are donoted as dashed lines. The direction % of the vector corresponding to given class is denoted as solid % line and its color distinguishes the class. % % Following algorithms can be tested: % fisherp - Modifed Perceptron learning rule. % fisherk - Modified Kozinec's algorithm. % % Control: % Algorithm - select algorithm for testing. % Iterations - number of iterations in one step. % % FIG2EPS - export screen to the PostScript file. % Load data - load input point sets from file. % Create data - invoke program for creating point sets. % Reset - set the tested algorithm to the initial state. % Play - run the tested algorithm. % Stop - stop the running algorithm. % Step - perform only one step. % Info - invoke the info box. % Close - close the program. % % See also FISHERP, FISHERK. % % Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac % (c) Czech Technical University Prague, http://cmp.felk.cvut.cz % Written Vojtech Franc (diploma thesis) 23.12.1999, 27.02.2000 % Modifications % 26-June-2001, V.Franc, comments changed. % 24. 6.00 V. Hlavac, comments polished. DATA_IDENT='Finite sets, Enumeration'; % file identifier BORDER=0.10; % minimal space between axis and any point MAX_CLASS=10; % if number of the arguments is less then 1, it means first call of this function. % Every other calls set up at least one argument. if nargin < 1, action = 'initialize'; end % what action is called ? switch lower(action) case 'initialize' % == Initialize user interface control and figure ======================= % == Figure ============================================================= left=0.1; width=0.8; bottom=0.1; height=0.8; hfigure=figure('Name','Fisher`s classifier', ... 'Visible','off',... 'NumberTitle','off', ... 'Units','normalized', ... 'Position',[left bottom width height],... 'tag','Fishdemo',... 'Units','normalized',... 'RendererMode','manual'); % == Axes - place for drawing =========================================== left=0.1; width=0.65; bottom=0.35; height=0.60; haxes1=axes(... 'Units','normalized', ... 'DrawMode','fast',... 'Box','on', ... 'UserData',[],... 'Position',[left bottom width height]); xlabel('feature x'); ylabel('feature y'); % == Comment window ===================================================== % Comment Window frame bottom=0.05; height=0.2; uicontrol( ... 'Style','frame', ... 'Units','normalized', ... 'Position',[left bottom width height], ... 'BackgroundColor',[0.5 0.5 0.5]); % Text label uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left height-0.01 width 0.05], ... 'BackgroundColor',[0.5 0.5 0.5], ... 'ForegroundColor',[1 1 1], ... 'String','Comment Window'); % Edit window border=0.01; hconsole=uicontrol( ... 'Style','edit', ... 'HorizontalAlignment','left', ... 'Units','normalized', ... 'Max',10, ... 'BackgroundColor',[1 1 1], ... 'Position',[left+border bottom width-2*border height-0.05], ... 'Enable','inactive',... 'String',''); % == Buttons =========================================================== % -- Export to EPS --------- width=0.1; left=0.75-width; bottom=0.95; height=0.04; hbtclose = uicontrol(... 'Units','Normalized', ... 'Callback','fig2eps(gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','FIG2EPS'); %---------------------------------- % Close button: close figure left=0.8; bottom=0.05; height=0.05; width=0.15; hbtclose = uicontrol(... 'Units','Normalized', ... 'Callback','close(gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Close'); % Info button: call stanard info box bottom=bottom+1.5*height; hbtinfo = uicontrol(... 'Units','Normalized', ... 'Callback','fishdemo(''info'',gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Info'); % Step button: perform one step, compute new hyperplane bottom=bottom+2.5*height; hbtstep = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Step', ... 'Callback','fishdemo(''step'',gcf)'); % Stop button: stop performing adaptation of hyperplane bottom=bottom+height; hbtstop = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Stop', ... 'Callback','set(gcbo,''UserData'',1)',... 'Enable','off'); % Play button: start up adaptation, step by step bottom=bottom+height; hbtplay = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Play', ... 'Callback','fishdemo(''play'',gcf)'); % Reset button: set up initial setting, zero step of adaptation bottom=bottom+height; hbtreset = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Reset', ... 'Callback','fishdemo(''reset'',gcf)'); % Create data bottom=bottom+1.5*height; hbtcreat = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Create data', ... 'Callback','fishdemo(''creatdata'',gcf)'); % Load data bottom=bottom+1*height; hbtload = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Load data', ... 'Callback','fishdemo(''getfile'',gcf)'); % == Popup menus ========================================================== % Popu up menu for selection among algorithms % Title bottom=0.95-height; htxalgo=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width height], ... 'String','Algorithm'); % Popup menu bottom=bottom-height; hpualgo=uicontrol( ... 'Style','popup', ... 'Units','normalized', ... 'Position',[left bottom width height], ... 'String',['Perceptron';'Kozinec ']); % == Edit line ========================================================= % # of iterations bottom=bottom-2*height; htxiter=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width 0.9*height], ... 'String','Iterations'); bottom=bottom-1*height; hediter = uicontrol(... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'Style','edit',... 'CallBack','fishdemo(''iterhandler'',gcf)',... 'String','1'); % ============================================================================== % Store handlers, note: first handler is reserved for separation line handlers=struct(... 'lines',struct('handlers',[],'alphas',[],'t',0),... 'btstep',hbtstep,... 'btclose',hbtclose,... 'btstop',hbtstop,... 'btplay',hbtplay,... 'btreset',hbtreset,... 'btload',hbtload,... 'btcreat',hbtcreat,... 'editer',hediter,... 'pualgo',hpualgo,... 'btinfo',hbtinfo,... 'axes1',haxes1,... 'console',hconsole); set(hfigure,'UserData',handlers) % Reset adaptation, t=0 fishdemo('reset',hfigure); % Put figure on desktop set(hfigure,'Visible','on'); drawnow; case 'creatdata' % == Invoke data set creator ============================================ creatset('finite',MAX_CLASS,'fishdemo','created',hfigure); case 'created' % == Load new created data set =========================================== % get handler and make this figure active figure(hfigure); h=get(hfigure,'UserData'); % get file name path=varargin{1}; name=varargin{2}; pathname=strcat(path,name); if checkdat(pathname,DATA_IDENT,2,0)==1, file.pathname=pathname; file.path=path; file.name=name; set(h.btload,'UserData',file); fishdemo('loadsets',hfigure); fishdemo('reset',hfigure); else errordlg('This file does not contain required data.','Bad file','modal'); end case 'iterhandler' % == Handler for edit line Iterations ==================================== h=get(hfigure,'UserData'); iter=round(str2num(get(h.editer,'String'))); if isempty(iter) | iter < 1, iter=1; end set(h.editer,'String',num2str(iter)); case 'getfile' % == Invoke standard open file dialog ==================================== % Opens file and checks if contains appropriate data, if yes loads data. h=get(hfigure,'UserData'); % change path to directory %% wres=what('fisher'); %% cd(wres.path); [name,path]=uigetfile('*.mat','Open file'); if name~=0, file.pathname=strcat(path,name); file.path=path; file.name=name; if checkdat(file.pathname,DATA_IDENT,2,0)==1, set(h.btload,'UserData',file); fishdemo('loadsets',hfigure); fishdemo('reset',hfigure); else errordlg('This file does not contain required data.','Bad file','modal'); end end case 'reset' % == Reset adaptation process, set t = 0 ===================================== h=get(hfigure,'UserData'); % get handlers sets=get(h.axes1,'UserData'); % loaded sets if isempty(sets)==0, % zeroes the parameters of separation lines h.lines.alphas=zeros(size(sets.K,2),2); h.lines.t=0; if isempty(h.lines.handlers)==1, % create the new lines h.lines.handlers=pfish2d(h.lines.alphas'); han=h.lines.handlers; else % change parameters of line h.lines.handlers=pfish2d(h.lines.alphas',h.lines.handlers); end % if h.lines==-1 % set up handlers set(hfigure,'UserData',h); file=get(h.btload,'UserData'); consoletext=sprintf('Step t=0\nNo separation line'); titletext=sprintf('File: %s, # of points K = %d',file.name,sum(sets.K)); else consoletext=sprintf('No data loaded.\nPress Load data button.\n'); titletext=''; pos=get(h.axes1,'Position'); fsize=min(pos(3),pos(4))/10; setaxis(h.axes1,[-1 1 -1 1]); builtin('text',0,0,'Press ''Load data'' button.',... 'HorizontalAlignment','center',... 'FontUnits','normalized',... 'Clipping','on',... 'FontSize',fsize); end % show comment set(h.console,'String',consoletext ); % print title pos=get(h.axes1,'Position'); fsize=(1-pos(2)-pos(4))*1; title(titletext,... 'VerticalAlignment','bottom',... 'HorizontalAlignment','left',... 'FontUnits','normalized',... 'Units','normalized',... 'Position',[0 1 0],... 'FontSize',fsize); case 'loadsets' % == Load sets from file ======================================================== % Get a file name from pop up menu according to index in variable named value. % Than clear axes, load new file and appear points from the file. h=get(hfigure,'UserData'); % uicontrol handlers % Clear axes clrchild(h.axes1); set(h.axes1, ... 'DrawMode','fast',... 'Box','on'); xlabel('feature x'); ylabel('feature y'); % No separation lines h.lines.handlers = []; set(hfigure,'UserData',h); % Get file name with sets X1 and X2 file=get(h.btload,'UserData'); % Load sets to structure sets sets=load(file.pathname); % store loaded sets set(h.axes1,'UserData',sets); % set axes according to current points win=cmpwin(min(sets.X'),max(sets.X'),BORDER,BORDER); %%%axis(win); setaxis(h.axes1,win); % plots sets of the points %% pplot(sets.X,sets.I); ppoints(sets.X,sets.I); case 'step' % == Perform one adaptation step ================================================ h=get(hfigure,'UserData'); % get handlers we will need... % get sets sets=get(h.axes1,'UserData'); if isempty(sets)==1, return; end % get the current time (number of steps) and the current solution (vectors alpha) t=h.lines.t; alphas=h.lines.alphas; % get # of iter. iter=max(1,str2num(get(h.editer,'String'))); switch get(h.pualgo,'Value') case 1 [alphas,solution,tplus1]=fisherp(sets.X,sets.I,size(sets.K,2),iter,t,alphas); case 2 [alphas,solution,tplus1]=fisherk(sets.X,sets.I,size(sets.K,2),iter,t,alphas); end t=tplus1; h.lines.t=t; h.lines.alphas=alphas; h.lines.handlers=pfish2d(alphas,h.lines.handlers); % If the time is the same like before adaptation, it means that soulution is found. if solution==0, % appear time and line text=sprintf('Step t=%d',t); else % Adaptation is done text=sprintf('Step t=%d\nSolution is found.',t); end %if t~=tplus1 set(h.console,'String',text ); % store handlers set(hfigure,'UserData',h); drawnow; case 'play' % == Start up adaptation process ================================================= % Perform adaptation step by step until the separation line is found or the stop % button is pushed down. h=get(hfigure,'UserData'); % get handlers % get sets sets=get(h.axes1,'UserData'); if isempty(sets)==1, return; end % Disable the buttons and the popup menus. set([h.btinfo h.btstep h.btclose h.btplay h.btreset h.btload h.btcreat h.pualgo],... 'Enable','off'); % Only stop button can be pushed down set(h.btstop,'Enable','on'); % get the current time (number of steps) and the current solution (vectors alpha) t=h.lines.t; alphas=h.lines.alphas; % The stop button was not pushed yet. set(h.btstop,'UserData',0); play=1; % Play - adaptation process while play==1 & get(h.btstop,'UserData')==0, % get # of iter. iter=max(1,str2num(get(h.editer,'String'))); switch get(h.pualgo,'Value') case 1 [alphas,solution,tplus1]=fisherp(sets.X,sets.I,size(sets.K,2),iter,t,alphas); case 2 [alphas,solution,tplus1]=fisherk(sets.X,sets.I,size(sets.K,2),iter,t,alphas); end t=tplus1; h.lines.t=t; h.lines.alphas=alphas; h.lines.handlers=pfish2d(alphas,h.lines.handlers); % If the time is the same like before adaptation, it means that soulution is found. if solution==0, % appear time and line text=sprintf('Step t=%d',t); else % Adaptation is done text=sprintf('Step t=%d\nSolution is found.',t); play=0; end %if t~=tplus1 set(h.console,'String',text ); % Flush the queue with graph. objects and events. drawnow; end % Enable buttons and popup menus set([h.btinfo h.btstep h.btclose h.btplay h.btreset h.pualgo h.btload h.btcreat],... 'Enable','on'); % disable stop button set(h.btstop,'Enable','off'); % store handlers set(hfigure,'UserData',h); case 'info' helpwin(mfilename); end % switch(action)